﻿#include "GateWayWidget.h"
#include "ui_GateWayWidget.h"
#include <QGridLayout>
#include "CommonFunc.h"
#include "DataInfo.h"
#include <QPushButton>
#include <QGridLayout>
#include <QSpacerItem>
#include <QFont>

GateWayWidget::GateWayWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GateWayWidget)
{
    ui->setupUi(this);

    _gridLayout = new QGridLayout();
    _gridLayout->setSpacing(30);
    _gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    _gridLayout->setContentsMargins(30, 50, 30, 50);
    _gridLayout->setHorizontalSpacing(40);
    _gridLayout->setVerticalSpacing(40);
    this->setLayout(_gridLayout);

    _verticalSpacer = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
//    gridLayout->addItem(verticalSpacer, 6, 1, 1, 1);

    _horizontalSpacer = new QSpacerItem(60, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
//    gridLayout->addItem(horizontalSpacer, 5, 1, 1, 3);

}

GateWayWidget::~GateWayWidget()
{
    delete ui;
}

void GateWayWidget::showGateWayInfo()
{
    _gridLayout->removeItem(_horizontalSpacer);
    _gridLayout->removeItem(_verticalSpacer);

    for (int i = 0; i < _vecButton.size(); ++i)
    {
        _gridLayout->removeWidget(_vecButton[i]);
        _vecButton[i]->hide();
        _vecButton[i]->deleteLater();
    }
    _vecButton.clear();

    if (g_vecGateWay.empty())
    {
        return;
    }

    int iCurRow = 0;

    for (int i = 0; i < g_vecGateWay.size(); ++i)
    {
        int j = 0;
        for (; j < _iColumn && i < g_vecGateWay.size(); ++j)
        {
            GateWay& info = g_vecGateWay[i++];
            QPushButton* toolButton = new QPushButton(this);
            toolButton->setObjectName("pushButton_gateWay");
            QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
            sizePolicy.setHorizontalStretch(0);
            sizePolicy.setVerticalStretch(0);
            sizePolicy.setHeightForWidth(toolButton->sizePolicy().hasHeightForWidth());
            toolButton->setSizePolicy(sizePolicy);
//            toolButton->setMinimumSize(QSize(160, 50));
            toolButton->setText(info.strName);
            QFont font1;
            font1.setPointSize(18);
//            font1.setBold(true);
            toolButton->setFont(font1);
            toolButton->setMinimumHeight(110);
            toolButton->setMaximumWidth(440);
            _gridLayout->addWidget(toolButton, iCurRow, j, 1, 1);
            toolButton->connect(toolButton, &QPushButton::clicked, [=](){
                g_iGateWayID = info.id;
                g_mapGroupIdToListSensor.clear();
                sendSensorDeviceRequest();
                executeFuncFromID(XUI_DeviceManage);
            });
            _vecButton.push_back(toolButton);
        }
//        qDebug() << j << " : " << _iColumn << "  i:  " << i;
        if (j < _iColumn && i >= g_vecGateWay.size())
        {
            _gridLayout->addItem(_horizontalSpacer, iCurRow, j, 1, _iColumn - j);
        }
        ++iCurRow;
    }

    _gridLayout->addItem(_verticalSpacer, iCurRow, 0, 1, 1);
}
